import { objDeepCopy,getCascaderObj ,getDateString} from "@/assets/js/common.js";
let Controller = {
	created() {
		this.init();
	},
	mounted() {
		
	},
	methods: {
		init() {
			this.getMsgAjax(this,this.$route.query.number)
		},
		//查询 详情信息
		getMsgAjax(that,orderNumber){
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'post',
					url:apiTest+'/supplier/whWarehouseDetail/outAndInDetails',
					data:{
						condition:{
							number:orderNumber  //单号
						}
					}
			    }).then((res)=>{
					console.log(res);
					// that.Model.getTypeOptionData = [];
					var data = res.data;
					if(data.status == 200){
						var obj = {};
						var result = data.result;
						if(result){
							/* tabFormInline */
							that.Model.whWarehouseId = result.whWarehouseId;
							
							that.Model.tabFormInline = [
								{
									label:'认证单号',
									value:result.number
								},
								{
									label:'认证机构',
									value:result.authenticationOrganization
								},
								{
									label:'认证类型',
									value:result.authenticationTyle
								}
							]
							var authGoodsList = result.authGoodsList;
							var obj;
							for (let i = 0; i < authGoodsList.length; i++) {
								
								
								obj = objDeepCopy(authGoodsList[i]);
								obj.mainQuantity = authGoodsList[i].mainQuantity;
								obj.remainingQuantity = authGoodsList[i].remainingQuantity;
								obj.status = true;
								obj.id = authGoodsList[i].id;
								if(authGoodsList[i].batchNo){
									obj.isBatchNo = true;
								}else{
									obj.isBatchNo = false;
								}
								that.Model.tableData.push(obj);
							}

						}
					}else{
			
					}
			
				});
		},
		
		//扫码入库 请求
		codeEntryAjax(that,params){
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'post',
					url:apiTest+'/supplier/whAllot/getGooods',
					data:params
			    }).then((res)=>{
					var data = res.data;
					if(data.status == 200){
						var obj = {};
						var result = data.result;
						if(result){
							obj = objDeepCopy(result.saleGoodsInfo);
							obj.canCount = result.remainingQuantity;
							obj.barCode = result.barCode;
							obj.createBy = result.createBy;
							obj.createTime = result.createTime;
							obj.id = result.id;
							obj.isDel = result.isDel;
							obj.mainQuantity = result.mainQuantity;
							obj.remainingQuantity = result.remainingQuantity;
							obj.sourceDetailsId = result.sourceDetailsId;
							obj.sourceDetailsTableName = result.sourceDetailsTableName;
							obj.status = result.status;
							obj.whWarehouseId = result.whWarehouseId;
							
							if(result.saleGoodsInfo.batchNo){
								obj.isBatchNo = true;
							}else{
								obj.isBatchNo = false;
							}
							that.Model.tableData.push(obj)
						}else{
							this.tips(this,'warning','未找到相关条码');
						}
					}else{
			
					}
			
				});
		},
		//扫码入库
		codeEntry(){
			var code = this.Model.searchCode;
			var whWarehouseId = this.Model.whWarehouseId;
			if(code.length == 0){
				this.tips(this,'warning','请输入条码或者商品编码');
				return;
			}
			var tableData = this.Model.tableData;
			var codeIndexOf = this.valueInData(code,tableData);
			var params = {
				keyword:code,
				whWarehouseId:whWarehouseId,
				count:1
			}
			if(codeIndexOf == -1){
				this.codeEntryAjax(this,params);
			}else{
				this.tips(this,'warning','条码已存在')
			}
		},
		//遍历 判断是否存在数组中
		valueInData(val,data){
			var indexof = -1;
			data.forEach(function(value,index,array){
				Object.keys(value).forEach(function(key){
					// console.log(key,value[key]);
					if(key == 'batchNo' && value[key] == val){
						indexof = 0;
						return;
					}
				});
			})
			return indexof;
		},
		//删除 表格数据
		deleteTableData(){
			var that = this;
			var TableSelectData = this.Model.TableSelectData;
			var tableData = this.Model.tableData;
			if(TableSelectData.length == 0){
				this.tips(this,'warning','请先勾选需要删除的数据');
				return;
			}
			this.$confirm('此操作将删除相应数据, 是否继续?', '提示', {
			  confirmButtonText: '确定',
			  cancelButtonText: '取消',
			}).then(() => {
				//需要发起请求 删除的行的 id数组
				var delIdArr = [];
				TableSelectData.forEach(function(value,index,array){
					tableData.forEach(function(val,idx,arr){
						/* 删除表格中的行 */
						if(value == val){
							that.Model.tableData.splice(idx,1);
							
							/* 若行的status为true（不是自己新加的，是进来数据库返回的数据），则将id添加到数组 */
							if(value.status == true){
								delIdArr.push(value.id);
							}
						}
						
					})
				})
				/* 若数组长度大于0，则发起请求删除*/
				if(delIdArr.length != 0){
					that.deleteDataAjax(that,delIdArr);
				}
			}).catch(() => {
			    
			});
		},
		//删除数据的请求
		deleteDataAjax(that,idArr){
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'post',
					url:apiTest+'/supplier/whAllot/deleteDetail',
					data:idArr
			    }).then((res)=>{
					var data = res.data;
					if(data.status == 200){
						that.tips(that,'success','删除成功')
					}
				});
		},
		//点击导入物料按钮
		importMaterial(){
			this.Model.importFileDialog = true;
		},
		//选择文件
		fileChange(){
			var fileObj = document.getElementById("fileup").files[0];
			var form = new FormData();
			form.append("file", fileObj);
			this.Model.upFileForm = form;
			var that = this;
			var obj = document.getElementById("fileup");
			var len = obj.files.length;
			for (var i = 0; i < len; i++) {
				var temp = obj.files[i].name;
				that.Model.fileupName = temp;
			}
		},
		//点击上传
		uploadExcel(){
			var apiTest = this.apiTest;
			var tableData = objDeepCopy(this.Model.tableData);
			var that = this;
			this.$ajax({
				method: 'post',
				url:apiTest+'/supplier/whWarehouseDetail/importExcelList',
				data:that.Model.upFileForm
			}).then((res)=>{
				console.log(res);
				var obj = {};
				if(res.data.status == 200){
					var result = res.data.result;
					if(result){
						for (let i = 0; i < result.length; i++) {
							var indexof = -1;
							obj = objDeepCopy(result[i]);
							if(tableData.length == 0){
								that.Model.tableData.push(obj);
							}else {
								tableData.forEach(function(value,index,array){
									if(value.batchNo == obj.batchNo){
										indexof = 0;
									}
								})
								if(indexof == -1){
									that.Model.tableData.push(obj);
								}
							}
							
						}
					}
					// that.tips(that,'success','导入成功！')
					setTimeout(function(){
						that.Model.importFileDialog = false;
					},1000);
				}else{
					that.tips(that,'warning','导入失败！')
				}
			});
		},
		//下载模板
		downloadExcel(){
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url:apiTest+'/supplier/whWarehouseDetail/getGoodsExcelInfo',
			}).then((res)=>{
				console.log(res);
				if(res.data.status == 200){
					var result = res.data.result;
					if(result.fileUrl){
						window.open(result.fileUrl);
					}
					
				}else{
					
				}
			});
		},
		//返回上一页
		back(){
			this.$router.push({
				path:'certificationStorageList',
				query:{
					
				}
			});
		},
		//提交订单
		onConfirmNew(){
			var arr = [];
			var obj = {};
			var tableData = this.Model.tableData;
			for (let i = 0; i < tableData.length; i++) {
				obj = {
					id: tableData[i].id,
					whWarehouseId: tableData[i].whWarehouseId,
					barCode: tableData[i].barCode,
					sourceDetailsId: tableData[i].sourceDetailsId,
					sourceDetailsTableName: tableData[i].sourceDetailsTableName,
					remainingQuantity: tableData[i].remainingQuantity,
					count:tableData[i].mainQuantity,
					status: tableData[i].status,
				}
				arr.push(obj);
			}
			if(tableData.length == 0){
				this.tips(this,'warning','请选择数据');
				return;
			}
			//提交的数据 formInline
			var formInline = this.Model.formInline;
			formInline.whWarehouseDetails = arr;
			formInline.id = this.Model.whWarehouseId;
			this.$confirm('此操作将提交新建订单, 是否继续?', '提示', {
			  confirmButtonText: '确定',
			  cancelButtonText: '取消',
			}).then(() => {
				this.confirmNewAjax(this,formInline);
			}).catch(() => {
			    
			});
		},
		//保存 提交调拨单 请求
		confirmNewAjax(that,params) {
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'post',
					url:apiTest+'/supplier/whWarehouse/addAuthInBill',
					data:params
			    }).then((res)=>{
					var data = res.data;
					if(data.status == 200){
						var result = data.result;
						that.tips(that,'success','操作成功')
					}else{
						that.tips(that,'warning',data.msg)
					}
			
				});
		},
		//直接出库
		onDirectOut() {
			var whWarehouseId = this.Model.whWarehouseId;
			var arr = [];
			var obj = {};
			var tableData = this.Model.tableData;
			for (let i = 0; i < tableData.length; i++) {
				obj = {
					id: tableData[i].id,
					barCode: tableData[i].barCode,
					whWarehouseId: tableData[i].whWarehouseId,
					sourceDetailsId: tableData[i].sourceDetailsId,
					sourceDetailsTableName: tableData[i].sourceDetailsTableName,
					remainingQuantity: tableData[i].remainingQuantity,
					count:tableData[i].mainQuantity
				}
				arr.push(obj);
			}
			if(tableData.length == 0){
				this.tips(this,'warning','请选择数据');
				return;
			}
			var params = {
				id:whWarehouseId,
				whWarehouseDetails:arr
			}
			this.$confirm('此操作将直接出库, 是否继续?', '提示', {
			  confirmButtonText: '确定',
			  cancelButtonText: '取消',
			}).then(() => {
				this.directOutAjax(this,params);
			}).catch(() => {
			    
			});
		},
		//直接出库 调拨单
		directOutAjax(that,params) {
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'post',
					url:apiTest+'/supplier/whWarehouse/confirmAuthIn',
					data:params
			    }).then((res)=>{
					var data = res.data;
					if(data.status == 200){
						var result = data.result;
						if(result == 1){
							that.tips(that,'success','操作成功')
						}
					}else{
						that.tips(that,'warning',data.msg)
					}
			
				});
		},
		
		
		//取消订单
		cancelOrder() {
			
		},
		showNotSureBarCodeP1() {
			
		},
		showNotSureBarCodeP2(){
			
		},
		toSearchNotSureBarCode() {
			
		},
		//
		selectedHighlight(data) {
			// 如果选中则添加高亮样式
			if (this.Model.TableSelectData.indexOf(data.row) > -1) {
				return {
					"background-color": "#FEF6EE"
				};
				return {}
			}
		},
		tips(that,type,message) {
		  that.$message({
		    message: message,
		    type: type
		  });
		},
		toggleTableDetail(val) {
			const obj = this.Model[val];
			if (obj.flag === true) {
				this.Model[val].src = require('@/assets/img/icon_collapse.png');
				this.Model[val].flag = false;
			} else {
				this.Model[val].src = require('@/assets/img/icon_unfold.png');
				this.Model[val].flag = true;
			}
		},
		handleSelectionChange(val) {
		  this.Model.TableSelectData = val;
		},
		handleCurrentChange(val) {
		  this.Model.pager.params.pageIndex = val;
		},
		handleSizeChange(val) {
		  this.Model.pager.params.pageSize = val;
		},
		cardClick(color) {
		  this.Model.cardSelected = color;
		},
		showChild(val) {
		  this.Model.searchFromData = val;
		},
		toSearch(val) {
			var params = {
					conditcion:this.Model.searchFromData
				}
			this.getCommodity(this,params)
		},
		toClear(val) {
		  this.Model.searchFromData = this.Model.defaultSearchFromData;
		},
		toExport(val) {
		  console.log(val);
		},
		
  },
  watch:{
  	$route:{
  	　　　handler(newValue, oldValue) {
  			if(newValue.query.number) {
  				this.getMsgAjax(this,newValue.query.number);
  			}
  	　　　},
  	　　　deep: true
  	},
  
  },
}
export default Controller;
